<template>
  <div class="home-container">
    <el-row :gutter="20">
      <el-col :span="8">
        <el-card class="stat-card" @click="navigateTo('/habits')">
          <template #header>
            <div class="card-header">
              <span>习惯打卡统计</span>
              <el-button text>
                <el-icon><ArrowRight /></el-icon>
              </el-button>
            </div>
          </template>
          <div class="stat-content">
            <div class="stat-item">
              <span>今日已打卡</span>
              <span class="stat-number">{{ todayCheckedHabits }}</span>
            </div>
            <div class="stat-item">
              <span>总习惯数</span>
              <span class="stat-number">{{ totalHabits }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="stat-card" @click="navigateTo('/todo')">
          <template #header>
            <div class="card-header">
              <span>待办事项统计</span>
              <el-button text>
                <el-icon><ArrowRight /></el-icon>
              </el-button>
            </div>
          </template>
          <div class="stat-content">
            <div class="stat-item">
              <span>待完成</span>
              <span class="stat-number">{{ pendingTodos }}</span>
            </div>
            <div class="stat-item">
              <span>已完成</span>
              <span class="stat-number">{{ completedTodos }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="stat-card" @click="navigateTo('/schedule')">
          <template #header>
            <div class="card-header">
              <span>日程统计</span>
              <el-button text>
                <el-icon><ArrowRight /></el-icon>
              </el-button>
            </div>
          </template>
          <div class="stat-content">
            <div class="stat-item">
              <span>今日日程</span>
              <span class="stat-number">{{ todayEvents }}</span>
            </div>
            <div class="stat-item">
              <span>明日日程</span>
              <span class="stat-number">{{ tomorrowEvents }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <el-col :span="16">
        <el-card class="chart-card" @click="navigateTo('/weeklyStats')">
          <template #header>
            <div class="card-header">
              <span>周报完成情况</span>
              <el-button text>
                <el-icon><ArrowRight /></el-icon>
              </el-button>
            </div>
          </template>
          <div class="mock-chart">
            <div class="mock-line-chart">
              <div class="mock-axis"></div>
              <div class="mock-line">
                <div class="mock-point" style="height: 30%"></div>
                <div class="mock-point" style="height: 45%"></div>
                <div class="mock-point" style="height: 60%"></div>
                <div class="mock-point" style="height: 80%"></div>
                <div class="mock-point" style="height: 70%"></div>
                <div class="mock-point" style="height: 90%"></div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="user-card">
          <template #header>
            <div class="card-header">
              <span>我的信息</span>
            </div>
          </template>
          <div class="user-info">
            <el-avatar :size="64" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
            <div class="user-details">
              <h3>{{ userInfo.username }}</h3>
              <p>管理员</p>
            </div>
          </div>
          <div class="quick-actions">
            <el-button round @click="navigateTo('/weekly')">填写周报</el-button>
            <el-button round @click="logout" type="danger">退出登录</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="recent-activity-card">
          <template #header>
            <div class="card-header">
              <span>近期活动</span>
            </div>
          </template>
          <el-timeline>
            <el-timeline-item timestamp="今天" placement="top" type="primary">
              <h4>习惯打卡：晨跑</h4>
              <p>已连续打卡 5 天</p>
            </el-timeline-item>
            <el-timeline-item timestamp="昨天" placement="top" type="success">
              <h4>完成待办：项目报告</h4>
              <p>按时完成了项目进度报告</p>
            </el-timeline-item>
            <el-timeline-item timestamp="前天" placement="top">
              <h4>添加新习惯：冥想</h4>
              <p>每天冥想15分钟</p>
            </el-timeline-item>
            <el-timeline-item timestamp="3天前" placement="top" type="warning">
              <h4>延期待办：技术分享</h4>
              <p>技术分享演讲延期到下周</p>
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useHabitsStore } from '@/store/habits'
import { useScheduleStore } from '@/store/schedule'
import { ArrowRight } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const habitsStore = useHabitsStore()
const scheduleStore = useScheduleStore()

// 统计数据
const todayCheckedHabits = ref(2)
const totalHabits = ref(3)
const pendingTodos = ref(5)
const completedTodos = ref(8)
const todayEvents = ref(2)
const tomorrowEvents = ref(3)

// 用户信息
const userInfo = computed(() => {
  try {
    return JSON.parse(localStorage.getItem('userInfo')) || { username: 'Guest' }
  } catch (e) {
    return { username: 'Guest' }
  }
})

onMounted(async () => {
  // 载入数据
  await Promise.all([
    habitsStore.fetchHabits(),
    scheduleStore.fetchEvents()
  ])

  // 更新统计数据
  updateStats()
})

const updateStats = () => {
  // 习惯统计
  todayCheckedHabits.value = habitsStore.habits.filter(h => h.checkedToday).length
  totalHabits.value = habitsStore.habits.length

  // 日程统计
  const today = new Date().toISOString().split('T')[0]
  const tomorrow = new Date(Date.now() + 86400000).toISOString().split('T')[0]
  
  todayEvents.value = scheduleStore.events.filter(e => 
    new Date(e.date).toISOString().split('T')[0] === today
  ).length
  
  tomorrowEvents.value = scheduleStore.events.filter(e => 
    new Date(e.date).toISOString().split('T')[0] === tomorrow
  ).length
}

const navigateTo = (path) => {
  router.push(path)
}

const logout = () => {
  ElMessageBox.confirm(
    '确定要退出登录吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    localStorage.removeItem('isLoggedIn')
    localStorage.removeItem('userInfo')
    router.push('/login')
  }).catch(() => {})
}
</script>

<style scoped>
.home-container {
  padding: 20px;
  background-color: #f9fbff;
}

.stat-card, .chart-card, .user-card, .recent-activity-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
  cursor: pointer;
}

.stat-card:hover, .chart-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stat-content {
  padding: 20px;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #1e5eff;
}

.user-info {
  display: flex;
  align-items: center;
  padding: 20px;
}

.user-details {
  margin-left: 20px;
}

.user-details h3 {
  margin: 0 0 5px 0;
  color: #1e5eff;
}

.user-details p {
  margin: 0;
  color: #888;
}

.quick-actions {
  display: flex;
  justify-content: space-around;
  padding: 0 20px 20px;
}

.mock-chart {
  padding: 20px;
  height: 200px;
}

/* 模拟图表样式与WeeklyStats.vue中相同 */
.mock-line-chart {
  width: 100%;
  height: 200px;
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: space-around;
}

.mock-axis {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1px;
  background-color: #dcdfe6;
}

.mock-line {
  display: flex;
  justify-content: space-between;
  width: 90%;
  height: 100%;
  align-items: flex-end;
}

.mock-point {
  width: 20px;
  background-color: #1e5eff;
  border-radius: 4px 4px 0 0;
  position: relative;
}

.mock-point::after {
  content: "";
  position: absolute;
  top: -6px;
  left: 50%;
  transform: translateX(-50%);
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #1e5eff;
}

:deep(.el-button--primary) {
  background-color: #1e5eff;
}

:deep(.el-timeline-item__node--primary) {
  background-color: #1e5eff;
}
</style> 